<?php

	class FormWidget extends EbWidget
	{
		static public $form_id = 1;
		
		public function render()
		{
			$fields = $this->fields;
			$method = is_null($this->method) ? 'GET' : $this->method;
			$action = is_null($this->action) ? '' : $this->action;
			$form_id = is_null($this->form_id) ? self::$form_id : $this->form_id;
			
			if (self::$form_id == 1)
			{
			?>
			<script type="text/javascript">
			<!--
			function validateForm(form)
			{
				var listInput = $('INPUT',form);

				for(var i = 0; i < listInput.length;i++)
				{
					//alert(listInput[i]);
				}
				 
				return true;
			}
			//-->
			</script>
			<?php 
			}
						
			echo '<form id="eb-form-'.$form_id.'" onsubmit="return validateForm(this)" action="'.$action.'" method="'.$method.'"';
			
			if (isset($this->enctype))
			{
				echo ' enctype="'.$this->enctype.'"';
			}
			
			echo '><table style="width:100%">';
						
			foreach ($fields AS $field)
			{
				$value 		= isset($field->value) ? $field->value : '';
				$message 	= isset($field->message) ? $field->message : '';
				
				if (isset($field->required) && $field->required)
				{
					$required = '*';
				}
				else
				{
					$required = '';
				}
				
				if (isset($field->itemRenderer))
				{
					$itemRenderer = parent::evalItemRenderer($field,$field->itemRenderer);
				}
				else
				{
					$itemRenderer = '<input required="true" type="text" name="'.$field->name.'" value="'.$value.'" style="width:90%;" />';
				}

				if (isset($field->label))
				{
					echo '<tr><td>'.$field->label.'</td><td>'.$itemRenderer.$required.'</td><td>'.$message.'</td></tr>';
				}
				else
				{
					echo $itemRenderer;
				}
			}
			
			echo '<tr><td colspan="2" align="center"><a class="minibutton" href="#" onclick="$(\'#eb-form-'.$form_id.'\').submit()"><span class="add">Valider</span></a><a class="minibutton" href="javascript:history.go(-1);"><span class="delete">Annuler</span></a></td></tr>';
			
			echo '</table></form>';
			
			self::$form_id++;
		}	
	}

?>